package com.lzw.demo.surfaceviewdemo.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.lzw.demo.surfaceviewdemo.R;

/**
 * SurfaceViewDemo
 * Created by lzw on 2018/12/21. 14:09:06
 * 邮箱：632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
public class MySurfaceView extends SurfaceView implements Runnable,SurfaceHolder.Callback{

    private static final String TAG = MySurfaceView.class.getSimpleName();
    private Canvas mCanvas;
    private boolean mIsDrawing ;
    private SurfaceHolder mSurfaceHolder;
    private Path mPath;
    private Paint mPaint;



    public MySurfaceView(Context context) {
        super(context);
        initView();
    }

    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        mSurfaceHolder = getHolder();
        mSurfaceHolder.addCallback(this);
        mPath = new Path();
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(10);
    }

    public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        
        String str = String.format(getResources().getString(R.string.car_run_distance),34.54);
        Log.i(TAG, "surfaceCreated: 格式化字符串="+str);
        mIsDrawing = true;
        new Thread(this).start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.i(TAG, "run: THreadId="+Thread.currentThread().getId());
        mIsDrawing = false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        Log.i(TAG, "onTouchEvent: x="+x+",y="+y);
        switch (event.getActionMasked()){
            case MotionEvent.ACTION_DOWN:
                mPath.moveTo(x,y);
                break;
            case MotionEvent.ACTION_MOVE:
                mPath.lineTo(x,y);
                break;
        }
        return true;
    }

    @Override
    public void run() {
        long start = System.currentTimeMillis();
        while (mIsDrawing){
            Log.i(TAG, "run: THreadId="+Thread.currentThread().getId());

            draw();

        }
        long end = System.currentTimeMillis();
        if ((end - start)<100){
            try {
                Thread.sleep(100-(end-start));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private synchronized void draw() {
        try {
            mCanvas = mSurfaceHolder.lockCanvas();
            Log.i(TAG, "draw: drawColor");
            mCanvas.drawColor(Color.WHITE);
            mCanvas.drawPath(mPath,mPaint);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {

            if (mCanvas != null){
                mSurfaceHolder.unlockCanvasAndPost(mCanvas);
            }
        }
    }
}
